package org.example.business;

import org.example.manage.MilkTeaManage;
import org.example.model.MilkTea;
import org.example.ui.administrator.MilkTeaAdminItem;

import javax.swing.*;
import java.awt.*;
import java.util.Comparator;
import java.util.List;

public class MilkManageBusiness {
    public static List<MilkTea> milkTeas;
    public static JPanel teaPanel;
    public static MilkTeaAdminItem[] adminItems;
    public static SpringLayout teaLayout;
    public static JScrollPane scrollPane;
    public static void setAdminItems(int type) {
        milkTeas = new MilkTeaManage().read();
        if (type == 2||type == 3||type == 0||type == 1) {
            milkTeas.sort(new Comparator<MilkTea>() {
                @Override
                public int compare(MilkTea o1, MilkTea o2) {
                    Double d1 = o1.price, d2 = o2.price;
                    if (type == 3)
                        return d2.compareTo(d1);
                    else return d1.compareTo(d2);
                }
            });
        }
        if (type == 4||type == 5) {
            milkTeas.sort(new Comparator<MilkTea>() {
                @Override
                public int compare(MilkTea o1, MilkTea o2) {
                    Integer d1 = o1.inventory, d2 = o2.inventory;
                    if (type == 4) {
                        return d2.compareTo(d1);
                    }
                    else return d1.compareTo(d2);
                }
            });
        }
        if (type == 6) {
            milkTeas = MilkTeaManage.listClassify(milkTeas);
        }
        teaPanel.removeAll();
        adminItems = new MilkTeaAdminItem[milkTeas.size()];
        for (int i = 0; i < adminItems.length; i++) {
            adminItems[i] = new MilkTeaAdminItem(milkTeas.get(i), i + 1);
            teaPanel.add(adminItems[i]);
            if (i == 0) {
                teaLayout.putConstraint(SpringLayout.NORTH, adminItems[i], 0, SpringLayout.NORTH, teaPanel);
                teaLayout.putConstraint(SpringLayout.SOUTH, adminItems[i], 100, SpringLayout.NORTH, teaPanel);
            }
            else {
                teaLayout.putConstraint(SpringLayout.NORTH, adminItems[i], 0, SpringLayout.SOUTH, adminItems[i-1]);
                teaLayout.putConstraint(SpringLayout.SOUTH, adminItems[i], 100, SpringLayout.SOUTH, adminItems[i-1]);
            }
            teaLayout.putConstraint(SpringLayout.WEST, adminItems[i], 0, SpringLayout.WEST, teaPanel);
            teaLayout.putConstraint(SpringLayout.EAST, adminItems[i], 0, SpringLayout.EAST, teaPanel);
        }
        if (type == 1) {
            for (int i = 1; i < 5; i++) {
                adminItems[0].textFields[i].setFocusable(true);
            }
            adminItems[0].setBackground(Color.CYAN);
            adminItems[0].isEdit = true;
            MilkTeaAdminItem.isSave = false;
        }
        teaPanel.setPreferredSize(new Dimension(300, 101 * milkTeas.size() + 80));
        teaPanel.setVisible(false);
        teaPanel.setVisible(true);
    }
}
